package com.xjcs.saplingplan.ui.activity

import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.xjcs.saplingplan.base.BaseVbActivity
import com.xjcs.saplingplan.databinding.ActivityChatAiBinding
import com.xjcs.saplingplan.model.bean.ChatMessage
import com.xjcs.saplingplan.ui.adapter.MessageAdapter

class ChatAiActivity: BaseVbActivity<ActivityChatAiBinding>()/*, WebSocketManager.IWebSocketListener*/ {

    private val messages = mutableListOf<ChatMessage>()
    private lateinit var msgAdapter: MessageAdapter

//    private val webSocketManager by lazy { WebSocketManager.getInstance(applicationContext) }

    override fun getViewBinding(): ActivityChatAiBinding {
        return ActivityChatAiBinding.inflate(layoutInflater)
    }

    override fun initUi(savedInstanceState: Bundle?) {
        initRecyclerView()
        initWebSocket()
        initListeners()
    }

    private fun initRecyclerView() {
        msgAdapter = MessageAdapter(messages)
        mBinding.chatRecyclerView.apply {
            layoutManager = LinearLayoutManager(this@ChatAiActivity)
            adapter = this@ChatAiActivity.msgAdapter
        }
    }

    private fun initWebSocket() {
//        webSocketManager.setConfig(
//            "ws://192.138.3.166:9028/ws/websocket",
//            mapOf(
////                "Authorization" to "Bearer ${getAuthToken()}",
//                "Authorization" to "Bearer",
//                "Device-ID" to Build.ID
//            ))
//        webSocketManager.addListener(this)
//        webSocketManager.connect()
    }

    private fun initListeners() {
        mBinding.sendButton.setOnClickListener {
//            val message = mBinding.messageEditText.text.toString().trim()
//            if (message.isNotEmpty()) {
//                webSocketManager.send(message)
//                addLocalMessage(message)
//                mBinding.messageEditText.setText("")
//            }
        }
    }

    private fun addLocalMessage(content: String) {
        messages.add(ChatMessage(
            content = content,
            isLocal = true,
            timestamp = System.currentTimeMillis()
        ))
        msgAdapter.notifyItemInserted(messages.lastIndex)
        mBinding.chatRecyclerView.smoothScrollToPosition(messages.lastIndex)
    }

    override fun registerObserver() {
    }

    override fun initListener() {
    }

    /*override fun onConnected() {
        runOnUiThread {
            Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onMessageReceived(message: String) {
        runOnUiThread {
            messages.add(ChatMessage(
                content = message,
                isLocal = false,
                timestamp = System.currentTimeMillis()
            ))
            msgAdapter.notifyItemInserted(messages.lastIndex)
            mBinding.chatRecyclerView.smoothScrollToPosition(messages.lastIndex)
        }
    }

    override fun onClosing(code: Int, reason: String) {
        showConnectionStatus("Closing: $reason")
    }

    override fun onDisconnected(code: Int, reason: String) {
        showConnectionStatus("Disconnected: $reason")
    }

    override fun onError(throwable: Throwable) {
        showConnectionStatus("Error: ${throwable.message}")
    }

    override fun onMaxRetryReached() {
        showConnectionStatus("Max retry attempts reached")
    }*/

    private fun showConnectionStatus(text: String) {
        runOnUiThread {
            Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
        }
    }

    /*override fun onDestroy() {
        webSocketManager.removeListener(this)
        webSocketManager.destroy()
        super.onDestroy()
    }*/

}